package com.share.example.javafx.jacob;

import com.share.common.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.File;

/**
 * jacob-1.20-x86.dll 文件必须放在javahome录下。
 * <p>
 * 放在C:\Windows\System32 目录下不起作用
 */
@Slf4j
public class LibInit {
    public static void init() {
        String dllName = "jacob-1.20-x86.dll";
        String javaHome = System.getProperty("java.home");
        File file = new File(javaHome + "/bin/" + dllName);
        if (file.exists()) {
            log.info("jacob库文件存在,dll={}", file.getAbsoluteFile());
        } else {
            try {
                File dllFile = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "dll/" + dllName);
                byte[] bytes = FileUtil.readByte(dllFile);
                FileUtil.writeByte(file, bytes);
            } catch (Exception e) {
                log.error("dll库文件读取异常dll={}", dllName, e);
            }
        }
    }
}
